package com.base.myproject.server.copy;


import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import com.base.myproject.server.database.PoolMan;
/**
 * 数据拷贝定义的类
 * 表名busicopy
 * @author zgw
 *2009-6-26 上午12:04:24
 * javazgw@gmail.com
 */
public class CopyBusi {

	String busicopyid = null;
	String busicopyname = null;
	String[] param;
	
	public CopyBusi(String busicopyid,String busicopyname,String[] param)
	{

		this.busicopyid = busicopyid;
		this.busicopyname = busicopyname;
		this.param = param;
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String copyall() throws Exception
	{
		String sql = "select copysql,tartable,formula,busicode,groupid from busicopy where busicopyid='"+busicopyid+"' and busicopyname='"+busicopyname+"'";
		PoolMan pm = new PoolMan();
		ResultSet rs = pm.executeQuery(sql);
		StringBuffer sbf = new StringBuffer();
		sbf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sbf.append("<root>");
		String copysql="";
		while (rs.next()) {
			copysql = rs.getString("copysql");

			String tartable = rs.getString("tartable");
			String formula = rs.getString("formula");
//			sbf.append(copyonetar(copysql, tartable, formula));
		}
		Document xmlDoc = null;
		xmlDoc = DocumentHelper.parseText(copysql);

		List l = xmlDoc.selectNodes("/root/basestore");
		
		Iterator iterator = l.iterator();
		while (iterator.hasNext()) {
			Node  node = (Node ) iterator.next();
		
			String name = (String)node.valueOf("@name");
			String type = (String)node.valueOf("@type");
			String tablesql = (String)node.valueOf("@sql");
			 Pattern p = Pattern.compile("\\{\\d\\}");

		     Matcher matcher = p.matcher(tablesql);
		     while(matcher.find())
		     {
		    	 
		    	 System.out.println("-"+matcher.group());
		    	 String str = matcher.group();
		    	 str = str.substring(1,	str.length()-1);
		    	 System.out.println(str);
		    	 int i = Integer.parseInt(str);
		    	 System.out.println(param[i]);
		    	 tablesql = tablesql.replace(matcher.group(), param[i]);
		     }
			
			sbf.append(copyonetar(tablesql, name,type, ""));
		}
		sbf.append("</root>");
		return new String(sbf);
	}
	/**
	 * 构造单行的赋值 最后被copyall所调用，形成最终的xml传送到客户端，由客户端负责赋值
	 * @param copysql
	 * @param sourcetable
	 * @param type
	 * @param formula
	 * @return
	 * @throws Exception
	 */
	private String copyonetar(String copysql,String sourcetable,String type,String formula) throws Exception
	{
		String sql = copysql;//"select 'zz' as addr,'zgw' as postcode from ccode  where icode = '1'";
		System.out.println("--"+copysql);
		
		PoolMan pm = new PoolMan();
		ResultSet rs = pm.executeQuery(sql);
		int columncount = rs.getMetaData().getColumnCount();
		StringBuffer sbf = new StringBuffer();
		//<basestore name='ccodelinkman_info' type='2'>
		sbf.append("<basestore name='"+sourcetable+"' type='"+type+"'>");
		while(rs.next())
		{
			sbf.append("<record>");
			for(int i=1;i<=columncount;i++)
			{
				String name = rs.getMetaData().getColumnName(i);
				String value = rs.getString(name);
//				<addr>旺sss</addr>
				sbf.append("<"+name+">"+value+"</"+name+">");
			}
			sbf.append("</record>");
			
		}
		sbf.append("</basestore>");
		return new String(sbf);
	}
public static void main(String[] argv) throws Exception
{
//	CopyBusi vv = new CopyBusi("1001","zz",null);
//	System.out.println(vv.copyall());
	String str1 = "w1,w2,w3,w4,,";
	System.out.println(str1.split(",").length);
	String strsql = "qewerp{2}{3}{0}";
	String[] strarr = new String[]{"1","2"};
	 Pattern p = Pattern.compile("\\{\\d\\}");

     Matcher matcher = p.matcher(strsql);
     while(matcher.find())
     {
    	 
    	 System.out.println("-"+matcher.group());
    	 String str = matcher.group();
    	String strtemp = str.substring(1,	str.length()-1);
    	 System.out.println(strtemp);
    	 int i = Integer.parseInt(strtemp);
    	 System.out.println(str1.split(",")[i]);
    	// strsql =  strsql.replace("{2}",	"ZGW");
    	 strsql = strsql.replace(Matcher.quoteReplacement(str) ,Matcher.quoteReplacement(str1.split(",")[i]));
   
     System.out.println(Matcher.quoteReplacement(str));
     System.out.println(Matcher.quoteReplacement(Matcher.quoteReplacement(str1.split(",")[i])));
     }
     System.out.println(strsql);
}
}
